package com.next.aappublicapp;

import java.io.UnsupportedEncodingException;

import org.apache.http.ParseException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Loader;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.next.aappublicapp.loaders.SwarajFeedLoader;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
public class SwarajActivity extends Activity {
	
	private TextView chapaterTitle;
	private WebView chapaterContent;
	private SwarajFeedLoader swarajFeedLoader;
	private LinearLayout loadingStatus;
	private ScrollView swarajForm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_swaraj);
		
		WebView mWebView = (WebView)findViewById(R.id.webView);
	    mWebView.getSettings().setJavaScriptEnabled(true);
	    mWebView.getSettings().setPluginsEnabled(true);
	    String language = getIntent().getExtras().getString("Language");
	    String hindi = this.getString(R.string.Hindi);
	    if(language == null){
	    	language = "English";
	    }
	    if(language.equals(hindi)){
	    	mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url=http://iacmumbai.org/sadmin/images/system_documents/category_files/12/Swaraj%20-%20By%20Arvind%20Kejriwal%20-%20Hindi.pdf");
	    }else{
	    	mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url=http://iacmumbai.org/sadmin/images/system_documents/category_files/13/Swaraj%20-%20By%20Arvind%20Kejriwal%20-%20English.pdf");	
	    }
	    
	    //Hiondi 
	    //mWebView.loadUrl("https://docs.google.com/file/d/0B1vuuiO3sog7NDFzVjdjM3ZIRjg/view?usp=sharing&embedded=true");
	    
		/*
		swarajFeedLoader = new SwarajFeedLoader(this);

		

		chapaterTitle = (TextView) findViewById(R.id.swarajChapterTitle);
		chapaterContent = (WebView) findViewById(R.id.swarajChapterContent);
		loadingStatus = (LinearLayout) findViewById(R.id.loading_status);
		swarajForm = (ScrollView) findViewById(R.id.swaraj_form);

		Bundle bundle = null;
		getLoaderManager().initLoader(0, bundle, this);
		loadingStatus.setVisibility(View.VISIBLE);
		swarajForm.setVisibility(View.GONE);
		*/
		Log.i("Ravi", "Start loading Loader");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.swaraj, menu);
		return true;
	}

	public Loader<String> onCreateLoader(int id, Bundle args) {
		return swarajFeedLoader;
	}

	public void onLoadFinished(Loader<String> arg0, String jsonResult) {
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(jsonResult);
			JSONObject feedJsonObject = jsonObject.getJSONObject("feed");
			JSONArray entriesArray = feedJsonObject.getJSONArray("entry");
			JSONObject onePost;
			//show the last entry
			onePost = entriesArray.getJSONObject(entriesArray.length() - 3);
			String content = onePost.getJSONObject("content").getString("$t");
			chapaterTitle.setText(onePost.getJSONObject("title").getString("$t"));
			//content = content.replaceAll("\\/", "/");
			String base64 = android.util.Base64.encodeToString(content.getBytes("UTF-8"), android.util.Base64.DEFAULT);
			chapaterContent.loadData(content, "text/html; charset=UTF-8", base64);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		loadingStatus.setVisibility(View.GONE);
		swarajForm.setVisibility(View.VISIBLE);
		
	}

	public void onLoaderReset(Loader<String> arg0) {

	}
}
